#include <TcpServer.h>

class EchoServer
{
public:
    EchoServer(const std::string &ip, int port)
        : server_(ip, port)
    {
        server_.setMessageCallback(
            [=](std::shared_ptr<TcpConnection> conn, Buffer& inBuffer) {
                this->onMessage(conn, inBuffer);
            }
        );
    }

    void start()
    {
        server_.Start();
    }

    void onMessage(const std::shared_ptr<TcpConnection> &conn, Buffer& inBuffer)
    {
        // printf("%s",msg);
        std::string msg = inBuffer.retrieveAll();
        std::cout << "onMessage: " << msg;
        conn->send(msg.data(),msg.length());
    }

private:
    TcpServer server_;
};

int main()
{
    EchoServer server("127.0.0.1", 9999);
    server.start();
}